package cn.lngaosu.lwyd.module.system.service.webinterfacelog;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.lngaosu.lwyd.module.system.controller.admin.webinterfacelog.vo.*;
import cn.lngaosu.lwyd.module.system.dal.dataobject.webinterfacelog.WebinterfaceLogDO;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;

import cn.lngaosu.lwyd.module.system.dal.mysql.webinterfacelog.WebinterfaceLogMapper;

import static cn.lngaosu.lwyd.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lngaosu.lwyd.framework.common.util.collection.CollectionUtils.convertList;
import static cn.lngaosu.lwyd.framework.common.util.collection.CollectionUtils.diffList;
import static cn.lngaosu.lwyd.module.system.enums.ErrorCodeConstants.*;

/**
 * 接口数据-请求日志 Service 实现类
 *
 * @author 张恒
 */
@Service
@Validated
public class WebinterfaceLogServiceImpl implements WebinterfaceLogService {

    @Resource
    private WebinterfaceLogMapper webinterfaceLogMapper;

    @Override
    public Long createWebinterfaceLog(WebinterfaceLogSaveReqVO createReqVO) {
        // 插入
        WebinterfaceLogDO webinterfaceLog = BeanUtils.toBean(createReqVO, WebinterfaceLogDO.class);
        webinterfaceLogMapper.insert(webinterfaceLog);
        // 返回
        return webinterfaceLog.getId();
    }

    @Override
    public void updateWebinterfaceLog(WebinterfaceLogSaveReqVO updateReqVO) {
        // 校验存在
        validateWebinterfaceLogExists(updateReqVO.getId());
        // 更新
        WebinterfaceLogDO updateObj = BeanUtils.toBean(updateReqVO, WebinterfaceLogDO.class);
        webinterfaceLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteWebinterfaceLog(Long id) {
        // 校验存在
        validateWebinterfaceLogExists(id);
        // 删除
        webinterfaceLogMapper.deleteById(id);
    }

    @Override
        public void deleteWebinterfaceLogListByIds(List<Long> ids) {
        // 校验存在
        validateWebinterfaceLogExists(ids);
        // 删除
        webinterfaceLogMapper.deleteByIds(ids);
        }

    private void validateWebinterfaceLogExists(List<Long> ids) {
        List<WebinterfaceLogDO> list = webinterfaceLogMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WEBINTERFACE_LOG_NOT_EXISTS);
        }
    }

    private void validateWebinterfaceLogExists(Long id) {
        if (webinterfaceLogMapper.selectById(id) == null) {
            throw exception(WEBINTERFACE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public WebinterfaceLogDO getWebinterfaceLog(Long id) {
        return webinterfaceLogMapper.selectById(id);
    }

    @Override
    public PageResult<WebinterfaceLogDO> getWebinterfaceLogPage(WebinterfaceLogPageReqVO pageReqVO) {
        return webinterfaceLogMapper.selectPage(pageReqVO);
    }

}